<?php
//自定义打印函数
function p($array){
    header('content-type:text/html;charset=utf-8');
    echo "<pre style='background-color: #f3f3f3;padding: 10px;font-family: 'ALKATIP Basma Tom''>";
        print_r($array);
    echo "</pre>";                    
}
//按排序排
/**
 * @param $data 数据数组
 * @return array
 */
function _sort($data){
    $list=array();
    foreach ($data as $v){
        if(0==$v['pid']){
            $v['lavel']=0;
            $list[]=$v;
            foreach ($data as $v1){
                if($v['id']==$v1['pid']){
                    $v1['lavel']=1;
                    $list[]=$v1;
                    foreach ($data as $v2){
                        if($v1['id']==$v2['pid']){
                            $v2['lavel']=2;
                            $list[]=$v2;
                            foreach ($data as $v3){
                                if($v2['id']==$v3['pid']){
                                    $v3['lavel']=3;
                                    $list[]=$v3;
                                    foreach ($data as $v4){
                                        if($v3['id']==$v4['pid']){
                                            $v4['lavel']=4;
                                            $list[]=$v4;
                                            foreach ($data as $v5){
                                                if($v4['id']==$v5['pid']){
                                                    $v5['lavel']=5;
                                                    $list[]=$v5;
                                                    foreach ($data as $v6){
                                                        if($v5['id']==$v6['pid']){
                                                            $v6['lavel']=6;
                                                            $list[]=$v6;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return $list;
}

function _no_delete($datas){
    $list=array();
    foreach ($datas as $k=>$data){
        if($data['pid']==0){
            foreach ($datas as $k1=>$v){
                if($data['id']==$v['pid']){
                    $data['nodel']=0;
                    $list[]=$data;
                }
            }
        }
    }
    return $list;
}
/*PHP版实现*/
/**
 * @param $str 重复的字符串
 * @param $num 重复的次数
 * @return string
 */
function _repeat($num){
//    return implode( $str, array_fill(0, $num+1, ''));
    return str_repeat("&nbsp;",$num*7);
}

/**
 * @param $info 提示内容
 * @param string $url 返回的地址
 */
function _check_alert($info,$url="",$icon=0){
    echo "<script> layer.alert('$info', {title:\"سېستىما ئەسكەرتىشى\",btn:\"بىلدىم\",icon:$icon,skin: 'layer-ext-moon',time:2000});location.href='$url'</script>";
}

/**
 * _substr（）标题截取函数
 * @param $string 要截取的内容
 * @return string
 */
function _substr($string){
    $string=trim($string);
    if(mb_strlen($string,"utf-8") > 100){
        $string=mb_substr($string,"0","100","utf-8")."...";
    }
    return $string;
}

/**
 * 更换皮肤
 */
//皮肤更换
 function dp_skin(){
    $skin=cookie("skin_name");
    if(!$skin){
        $skin="blue";
    }
    echo $skin;
}

////获得访客浏览器类型
function GetBrowser(){
    if(!empty($_SERVER['HTTP_USER_AGENT'])){
        $br = $_SERVER['HTTP_USER_AGENT'];
        if (preg_match('/MSIE/i',$br)) {
            $br = 'MSIE';
        }elseif (preg_match('/Firefox/i',$br)) {
            $br = 'Firefox';
        }elseif (preg_match('/Chrome/i',$br)) {
            $br = 'Chrome';
        }elseif (preg_match('/Safari/i',$br)) {
            $br = 'Safari';
        }elseif (preg_match('/Opera/i',$br)) {
            $br = 'Opera';
        }else {
            $br = 'Other';
        }
        return $br;
    }else{return "获取浏览器信息失败！";}
}

////获得访客浏览器语言
function GetLang(){
    if(!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
        $lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
        $lang = substr($lang,0,5);
        if(preg_match("/zh-cn/i",$lang)){
            $lang = "简体中文";
        }elseif(preg_match("/zh/i",$lang)){
            $lang = "繁体中文";
        }else{
            $lang = "English";
        }
        return $lang;

    }else{return "获取浏览器语言失败！";}
}

////获取访客操作系统
function GetOs(){
    if(!empty($_SERVER['HTTP_USER_AGENT'])){
        $OS = $_SERVER['HTTP_USER_AGENT'];
        if (preg_match('/win/i',$OS)) {
            $OS = 'Windows';
        }elseif (preg_match('/mac/i',$OS)) {
            $OS = 'MAC';
        }elseif (preg_match('/linux/i',$OS)) {
            $OS = 'Linux';
        }elseif (preg_match('/unix/i',$OS)) {
            $OS = 'Unix';
        }elseif (preg_match('/bsd/i',$OS)) {
            $OS = 'BSD';
        }else {
            $OS = 'Other';
        }
        return $OS;
    }else{return "获取访客操作系统信息失败！";}
}

////获得访客真实ip
function Getip(){
    if(!empty($_SERVER["HTTP_CLIENT_IP"])){
        $ip = $_SERVER["HTTP_CLIENT_IP"];
    }
    if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ //获取代理ip
        $ips = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
    }
    if($ip){
        $ips = array_unshift($ips,$ip);
    }

    $count = count($ips);
    for($i=0;$i<$count;$i++){
        if(!preg_match("/^(10|172\.16|192\.168)\./i",$ips[$i])){//排除局域网ip
            $ip = $ips[$i];
            break;
        }
    }
    $tip = empty($_SERVER['REMOTE_ADDR']) ? $ip : $_SERVER['REMOTE_ADDR'];
    if($tip=="127.0.0.1"){ //获得本地真实IP
        return $this->get_onlineip();
    }else{
        return $tip;
    }
}

////获得本地真实IP
function get_onlineip() {
    $mip = file_get_contents("http://city.ip138.com/city0.asp");
    if($mip){
        preg_match("/\[.*\]/",$mip,$sip);
        $p = array("/\[/","/\]/");
        return preg_replace($p,"",$sip[0]);
    }else{return "获取本地IP失败！";}
}

////根据ip获得访客所在地地名
function Getaddress($ip=''){
    if(empty($ip)){
        $ip = $this->Getip();
    }
    $ipadd = file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=".$ip);//根据新浪api接口获取
    if($ipadd){
        $charset = iconv("gbk","utf-8",$ipadd);
        preg_match_all("/[\x{4e00}-\x{9fa5}]+/u",$charset,$ipadds);

        return $ipadds;   //返回一个二维数组
    }else{return "addree is none";}
}
/**
 * 把预定义的字符转换为 HTML 实体
 * @param $string
 * @return string
 */
function safe_replace($string) {
    return htmlspecialchars(str_replace("\\","",$string), ENT_QUOTES);
}
function gpc_get($str){
    return preg_replace("/(fopen|union|update|table|query|shel|from|insert|global|mysql|select|array|outfile|char|load_file|ascii|eval|exe|delete|unlink|copy|require|include|=|%|;|:|tanha_|>|<|cmd)/isU","",$str);
}


function _strpos($str){
    $index=stripos($str,"/");
    if(!strpos($str,"Application")){
        switch ($index){
            case 0:
                return 'U'. str_replace(substr($str,0,2),"",$str);
                break;
            case 7:
                return 'Up'. str_replace(substr($str,0,2),"",$str);
                break;
            default:
                return $str;
                break;
        }
    }else{
        switch ($index){
            case 0:
                return 'A'. str_replace(substr($str,0,2),"",$str);
                break;
        }
    }

}
/**
 * _substr（）标题截取函数
 * @param $string 要截取的内容
 * @return string
 */
function _substr_title($string){
    if(mb_strlen($string,"utf-8") > 14){
        $string=mb_substr($string,"0","27","utf-8")."‥";
    }
    return $string;
}

function ads_elan($id){
   return \Home\Controller\Home2017Controller::ads($id);
}
//输出json
function json_encode_unicode($text) {
    if(phpversion()>'5.4.0'){
        return json_encode($text, JSON_UNESCAPED_UNICODE);
    }
    return str_replace(array("\u0644\u0627","\u0628","\u067e","\u062a","\u062c","\u0686","\u062e","\u062f","\u0631","\u0632","\u0698","\u0633","\u0634","\u063a","\u0641","\u0642","\u0643","\u06af","\u06ad","\u0644","\u0645","\u0646","\u06be","\u06cb","\u064a","\u0627","\u0649","\u06d5","\u0648","\u06c8","\u06d0","\u06c7","\u0626","\u06c6","\u061f","\u060c","\u0640","\u061b","\u203a","\u2039","\u00ab","\u00bb","\u200f","\u061c","\u202b"),array("لا","ب","پ","ت","ج","چ","خ","د","ر","ز","ژ","س","ش","غ","ف","ق","ك","گ","ڭ","ل","م","ن","ھ","ۋ","ي","ا","ى","ە","و","ۈ","ې","ۇ","ئ","ۆ","؟","،","ـ","؛","›","‹","«","»","","",""),json_encode($text));
}
/**
 * 产生随机字符串
 * @param $length
 * @param string $chars
 * @return string
 */
function random($length, $chars='0123456789'){
    $arr=str_split($chars);
    shuffle($arr);
    return implode("",array_slice($arr,0,$length));
}
/**
 * 产生随机字符串
 * @param int $lenth
 * @return string
 */
function create_randomstr($lenth = 6) {
    return random($lenth, '123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ');
}
/**
 * 时间格式化
 * @param $time
 * @return string
 */
function DateFormat($time) {
    $t=time()-$time; /* 时间格式化 */
    $f=array('31536000'=>' يىل','2592000'=>' ئاي','604800'=>' ھەپتە','86400'=>' كۈن','3600'=>' سائەت','60'=>' مېنۇت','1'=>' سىكۇنت');
    foreach ($f as $k=>$v){
        if (0!=$c=floor($t/(int)$k)){
            if ($k >= 3600){
                return $c.$v.' ئىلگىرى';
            }else{
                return 'باياتىن';
            }
        }
    }/* foreach */
    return 'باياتىن';
}

/**
 * 时间格式化(扩展区)
 * @param $time
 * @return string
 */
function DateFormat_convert($time) {
    $t=time()-$time; /* 时间格式化 */
    $f=array('31536000'=>' ﻳﯩﻞ','2592000'=>' ﺋﺎﻱ','604800'=>' ﮪﻪﭘﺘﻪ','86400'=>' ﻛﯜﻥ','3600'=>' ﺳﺎﺋﻪﺕ','60'=>' ﻣﯧﻨﯘﺕ','1'=>' ﺳﯩﻜﯘﻧﺖ');
    foreach ($f as $k=>$v){
        if (0!=$c=floor($t/(int)$k)){
            if ($k >= 3600){
                return $c.$v.' ﺋﯩﻠﮕﯩﺮﻯ';
            }else{
                return 'ﺑﺎﻳﺎﺗﯩﻦ';
            }
        }
    }/* foreach */
    return 'ﺑﺎﻳﺎﺗﯩﻦ';
}
/*移动端判断*/
/**
 * @return bool 判断是否移动端
 */
function isMobile()
{
    // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
    if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
    {
        return true;
    }
    // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
    if (isset ($_SERVER['HTTP_VIA']))
    {
        // 找不到为flase,否则为true
        return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
    }
    // 脑残法，判断手机发送的客户端标志,兼容性有待提高
    if (isset ($_SERVER['HTTP_USER_AGENT']))
    {
        $clientkeywords = array ('nokia',
            'sony',
            'ericsson',
            'mot',
            'samsung',
            'htc',
            'sgh',
            'lg',
            'sharp',
            'sie-',
            'philips',
            'panasonic',
            'alcatel',
            'lenovo',
            'iphone',
            'ipod',
            'blackberry',
            'meizu',
            'android',
            'netfront',
            'symbian',
            'ucweb',
            'windowsce',
            'palm',
            'operamini',
            'operamobi',
            'openwave',
            'nexusone',
            'cldc',
            'midp',
            'wap',
            'mobile'
        );
        // 从HTTP_USER_AGENT中查找手机浏览器的关键字
        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))
        {
            return true;
        }
    }
    // 协议法，因为有可能不准确，放到最后判断
    if (isset ($_SERVER['HTTP_ACCEPT']))
    {
        // 如果只支持wml并且不支持html那一定是移动设备
        // 如果支持wml和html但是wml在html之前则是移动设备
        if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html'))))
        {
            return true;
        }
    }
    return false;
}

/**
 * @param $num
 * @return int
 */
//分页的时候用：上一页
 function fenye_elix($num){
    $num--;
    if($num<1){
        $num=$num;
        return 1;
    }
     return $num;
}
//分页的时候用：下一页
 function fenye_koxux($num,$max){
    $num++;
    if($num>=$max){
        $num=$max;
        return $max;
    }
    return $num;
}

//ajax返回json
/**
 * @param $status 状态
 * @param $message title
 */
function response_jison_exit($status,$message,$id){
     $data=array('status'=>$status,'message'=>$message,'id'=>$id);
     return json_encode($data);
}
//
/**
 * @param $string
 * @return bool|string artuk url ni keswitix
 */
function _substr_url($string){
    return substr($string,30);
}

/**
 * 获取当前页面完整URL地址
 */
function get_url() {
    $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
    $php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
    $path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
    $relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : $path_info);
    return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
}